const {getLeaseList, createLease, updateLease, delLease, getLeaseOne} = require('../service/lease')
const {updateAsk} = require('../service/ask')
const {updateHouse} = require('../service/house')
const {SuccessModel, ErrorModel} = require('../model/ResModel')
const {addRoleFailInfo} = require('../model/ErrorInfo')
const {flatData} = require('../utils/flatData')

const getLease = async ({status, createTime = [], pageIndex = 0, pageSize = 10}) => {
  const result = await getLeaseList({
    status,
    createTime,
    pageIndex,
    pageSize
  })
  // result.data = flatData(result.data)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getLeaseMy = async (ctx, {status, createTime = [], pageIndex = 0, pageSize = 10}) => {
  const result = await getLeaseList({
    askId: ctx.session.user.user.id,
    status,
    createTime,
    pageIndex,
    pageSize
  })
  // result.data = flatData(result.data)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getLeaseYours = async (ctx, {status, createTime = [], pageIndex = 0, pageSize = 10}) => {
  const result = await getLeaseList({
    ownerId: ctx.session.user.user.id,
    status,
    createTime,
    pageIndex,
    pageSize
  })
  // result.data = flatData(result.data)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getLeaseById = async (id) => {
  let result = await getLeaseOne(+id)
  // result = flatData(result)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const postLease = async ({content, askId, status = 1}) => {
  const result = await createLease({
    content,
    askId,
    status
  })
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const patchLease = async ({content, status, times, id}) => {
  const result = await updateLease({content, status, id})
  if (result && status == 6 && times && times.length) {
    const result1 = await getLeaseOne(id)
    const result2 = await updateAsk({id: result1.ask.id, startTime: times[0], endTime: times[1]})
    return result2 ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
  }
  if (result && status == 6) {
    const result1 = await getLeaseOne(id)
    const result2 = await updateHouse({status: 3, id: result1.ask.house.dataValues.id, askId: result1.ask.askId})
    return result2 ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
  }
  if (result && (status == 4 || status == 5)) {
    const result1 = await getLeaseOne(id)
    const result2 = await updateHouse({status: 1, id: result1.ask.house.dataValues.id, askId: null})
    return result2 ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
  }
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const deleteLease = async (id) => {
  const result = await delLease(id)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}


module.exports = {
  getLease,
  getLeaseById,
  postLease,
  patchLease,
  deleteLease,
  getLeaseMy,
  getLeaseYours
}
